package com.westa.smallshop.data.source.remote;

import android.content.Context;
import android.support.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.Response;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.data.HttpError;
import com.westa.smallshop.data.source.GoodsItemDataSource;
import com.westa.smallshop.util.HttpUtils;
import com.westa.smallshop.util.UserSPUtils;
import com.westa.smallshop.util.volley.FastJsonArrayRequest;
import com.westa.smallshop.util.volley.FastJsonRequest;
import com.westa.smallshop.util.volley.VolleySingleton;

import java.util.List;

import timber.log.Timber;

/**
 * Created by cy on 11/4/16.
 */

public class GoodsItemRemoteDataSource implements GoodsItemDataSource {

    private static final String GOODS_TAG = "goods_tag";
    private static final String PASSWORD_VERIFY_TAG = "password_verify_tag";
    private static final String REQUEST_UPLOAD_GOODS_URL = HttpUtils.getHost() + "goodsUpload";
    private static final String REQUEST_GOODS_LIST = HttpUtils.getHost() + "goodsList";
    private static final String REQUEST_GOODS_ITEM_URL = HttpUtils.getHost() + "goodsDetail";
    private static final String REQUEST_GOODS_IN_CART_URL = HttpUtils.getHost() + "queryCart";
    private static final String REQUEST_ADD_GOODS_TO_CART = HttpUtils.getHost() + "addCart";
    private static final String REQUEST_ORDER_SUBMIT_URL = HttpUtils.getHost() + "orderSubmit";
    private static final String REQUEST_SEARCH_GOODS_URL = HttpUtils.getHost() + "search";
    private static final String REQUEST_DELETE_FROM_CART_URL = HttpUtils.getHost() + "deleteFromCart";
    private static final String REQUEST_LOGIN_URL = HttpUtils.getHost() + "login";
    private Context mContext;

    public GoodsItemRemoteDataSource(Context context) {
        mContext = context;
    }

    @Override
    public void cancelLoadRequest() {
        VolleySingleton.cancelRequestForStartWithTag(GOODS_TAG);
    }

    @Override
    public void getGoods(@LoadType final int loadType, String currentPage, final LoadListCallBack<GoodsItem> loadListCallBack) {
        FastJsonArrayRequest<GoodsItem> request = new FastJsonArrayRequest<GoodsItem>(REQUEST_GOODS_LIST,
                GoodsItem.class,
                new Response.Listener<List<GoodsItem>>() {
                    @Override
                    public void onResponse(List<GoodsItem> response) {

                        if (loadType == LOAD_MORE) {
                            loadListCallBack.onLoadMoreDataSuccessful(response);
                        } else {
                            loadListCallBack.onRefreshDataSuccessful(response);
                        }
                    }
                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadListCallBack.onLoadDataFailed();
                    }
                });
        request.addParam("goodsListPage", currentPage);
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, GOODS_TAG);
    }

    @Override
    public void addGoodsToCart(int goodsId, final LoadCallBack<JSONObject> loadCallBack) {

        FastJsonRequest<JSONObject> request = new FastJsonRequest<>(Request.Method.GET, REQUEST_ADD_GOODS_TO_CART,
                JSONObject.class,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        loadCallBack.onLoadDataSuccessful(response);
                    }
                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadCallBack.onLoadDataFailed();
                    }
                });
        request.addParam("goods_id", String.valueOf(goodsId));
        request.addParam("account", UserSPUtils.getAccount(mContext));
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, GOODS_TAG);
    }

    @Override
    public void getGoodsInCart(final LoadListCallBack<GoodsItem> loadListCallBack) {

        FastJsonArrayRequest<GoodsItem> request = new FastJsonArrayRequest<GoodsItem>(REQUEST_GOODS_IN_CART_URL,
                GoodsItem.class,
                new Response.Listener<List<GoodsItem>>() {
                    @Override
                    public void onResponse(List<GoodsItem> response) {

                        loadListCallBack.onRefreshDataSuccessful(response);
                    }
                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadListCallBack.onLoadDataFailed();
                    }
                });
        request.addParam("account", UserSPUtils.getAccount(mContext));
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, GOODS_TAG);
    }

    @Override
    public void searchGoods(String keyWord, final LoadListCallBack<GoodsItem> loadListCallBack) {

        FastJsonArrayRequest<GoodsItem> request = new FastJsonArrayRequest<GoodsItem>(REQUEST_SEARCH_GOODS_URL,
                GoodsItem.class,
                new Response.Listener<List<GoodsItem>>() {
                    @Override
                    public void onResponse(List<GoodsItem> response) {

                            loadListCallBack.onRefreshDataSuccessful(response);
                    }
                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadListCallBack.onLoadDataFailed();
                    }
                });
        request.addParam("keyword", keyWord);
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, GOODS_TAG);
    }

    @Override
    public void verifyPassword(String password, final LoadCallBack<JSONObject> loadCallBack) {

        FastJsonRequest<JSONObject> request = new FastJsonRequest<>(Request.Method.GET, REQUEST_LOGIN_URL,
                JSONObject.class,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        loadCallBack.onLoadDataSuccessful(response);
                    }
                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadCallBack.onLoadDataFailed();
                    }
                });

        request.addParam("account", UserSPUtils.getAccount(mContext));
        request.addParam("password", password);
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, PASSWORD_VERIFY_TAG);
    }

    @Override
    public void cancelVerifyPassword() {
        VolleySingleton.cancelRequestForStartWithTag(PASSWORD_VERIFY_TAG);
    }

    @Override
    public void deleteFromCart(int goodsId, final LoadCallBack<JSONObject> loadCallBack) {

        FastJsonRequest<JSONObject> request = new FastJsonRequest<>(Request.Method.GET, REQUEST_DELETE_FROM_CART_URL,
                JSONObject.class,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        loadCallBack.onLoadDataSuccessful(response);
                    }
                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadCallBack.onLoadDataFailed();
                    }
                });
        request.addParam("goods_id", String.valueOf(goodsId));
        request.addParam("account", UserSPUtils.getAccount(mContext));
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, GOODS_TAG);
    }

    @Override
    public void getGoodsItem(int goodsId, final LoadCallBack<GoodsItem> loadCallBack) {
        FastJsonRequest<GoodsItem> request = new FastJsonRequest<GoodsItem>(Request.Method.GET,
                REQUEST_GOODS_ITEM_URL,
                GoodsItem.class,
                new Response.Listener<GoodsItem>() {
                    @Override
                    public void onResponse(GoodsItem response) {
                        loadCallBack.onLoadDataSuccessful(response);
                    }

                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadCallBack.onLoadDataFailed();
                    }
                });
        request.addParam("goods_id", String.valueOf(goodsId));
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, GOODS_TAG);
    }

    @Override
    public void uploadGoods(GoodsItem goodsItem, String image, final LoadCallBack<JSONObject> loadCallBack) {
//        String goods_name, String goods_desc, String goods_price, String goods_pic,
        FastJsonRequest<JSONObject> request = new FastJsonRequest<>(Request.Method.GET, REQUEST_UPLOAD_GOODS_URL,
                null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {

                        loadCallBack.onLoadDataSuccessful(response);
                    }
                },
                new HttpUtils.ErrorListener(mContext) {

                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadCallBack.onLoadDataFailed();
                    }
                });

        request.addParam("goods_imgPath", image);
        request.addParam("goods_price", String.valueOf(goodsItem.getGoods_price()));
        request.addParam("goods_name", goodsItem.getGoods_name());
        request.addParam("account", UserSPUtils.getAccount(mContext));
        request.addParam("goods_desc", goodsItem.getGoods_desc());

        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, GOODS_TAG);
    }

    @Override
    public void orderSubmit(final LoadCallBack<JSONObject> loadCallBack) {

        FastJsonRequest<JSONObject> request = new FastJsonRequest<>(Request.Method.GET, REQUEST_ORDER_SUBMIT_URL,
                JSONObject.class,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        loadCallBack.onLoadDataSuccessful(response);
                    }
                },
                new HttpUtils.ErrorListener(mContext) {
                    @Override
                    public void onErrorData(@Nullable HttpError httpError) {
                        loadCallBack.onLoadDataFailed();
                    }
                });

        request.addParam("account", UserSPUtils.getAccount(mContext));
        Timber.e(request.getUrl());
        VolleySingleton.addRequest(request, GOODS_TAG);
    }
}
